<?php

namespace app\index\model;
use app\common\entity\Ucpaas;

use app\index\model\aliyun\api_demo\SmsDemo;
use think\facade\Session;
use app\index\model\aliyun\AlibabaCloud\Client\AlibabaCloud;
use app\index\model\aliyun\AlibabaCloud\Client\Exception\ClientException;
use app\index\model\aliyun\AlibabaCloud\Client\Exception\ServerException;


class SendCode
{
    public $mobile;
    public $code;
    public $type;

    public function __construct($mobile, $type)
    {
        $this->mobile = $mobile;
        $this->type = $type;
    }

    public function send()
    {
        $this->setCode();
        //调用发生接口
//        if ($this->sendCode()) {
//            $this->saveCode(); //保存code值到session值
//            return true;
//        }
        // 使用阿里云短信
        if ($this->sendSms()) {
            $this->saveCode(); //保存code值到session值
            return true;
        }
        return false;
    }

    public function getSessionName()
    {
        $sessionNames = [
            'register' => 'register_code_',
            'change-password' => 'change-password_',
            'market' => 'market_sale_',
            'market_sale' => 'market_sale_ta_',
            'updzfb' => 'upd_user_zfb_',
            'updcard' => 'upd_user_card_',
            'info' => 'info_'
        ];

        return $sessionNames[$this->type] . $this->mobile;
    }

    private function getCode()
    {
        return Session::get($this->getSessionName());
    }

    private function setCode()
    {
        $this->code = mt_rand(100000, 999999);
    }

    private function saveCode()
    {
        Session::set($this->getSessionName(), $this->code);
    }
    /*
    * 发送短信
    * @return stdClass
    */
    public function sendSms() {
        $code = $this->code;//参数
        $mobile = $this->mobile;
        $data = SmsDemo::sendSms($code,$mobile);
        return $data;
    }


    private function sendCode()
    {
        $code = $this->code;//参数
        //$content = '本次验证码为'.$code.'，请于3分钟内正确输入，切勿泄露他人。【金运来】';
        $content = '本次验证码为'.$code.'，请于3分钟内正确输入，切勿泄露他人。【GYG商城】';
        //$content = '【卡商天下】本次验证码为'.$code.'，请于3分钟内正确输入，切勿泄露他人。';
        $content = urlencode($content);
        $account = 'gd009533';
        $password = strtoupper(md5('gd00953333', false));
        $mobile = $this->mobile;

        $url = "https://dx.ipyy.net/smsJson.aspx?action=send&userid=&account=$account&password=$password&mobile=$mobile&content=$content&sendTime=&extno=";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);
        curl_close($curl);
        //print_r($data);
        return $data;
    }

    public function sendInfo($info)
    {

        $code = $this->code;//参数
        $content = $info;//'【卡商天下】本次验证码为:'.$code.',，请于3分钟内正确输入，切勿泄露他人';
        $content = urlencode($content);
        $account = 'gd009533';
        $password = strtoupper(md5('gd00953333', false));
        $mobile = $this->mobile;

        $url = "https://dx.ipyy.net/smsJson.aspx?action=send&userid=&account=$account&password=$password&mobile=$mobile&content=$content&sendTime=&extno=";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;


        /*//填写在开发者控制台福多多上的Account Sid
        $options['accountsid']='fe5c3f5ba5194c97a2a10fd424c9c588';
        //填写在开发者控制台福多多上的Auth Token
        $options['token']='9e6b8ff304b56a91ad447c1ba78a4a83';

        $appid = "82323c3eb9814a8d962b4b84d45f24b3";    //应用的ID，
        $templateid = "79576";    //查看该模板ID
        $param = $info;//参数
        $mobile = $this->mobile;
        $uid = "";
        $ucpass = new Ucpaas($options);
        $data = $ucpass->SendSms($appid,$templateid,$param,$mobile,$uid);
        return true;*/
    }

    public function checkCode($code)
    {
        $trueCode = $this->getCode();

        if ($trueCode == $code) {
            uj::delete($this->getSessionName());
            return true;
        }

        return false;
    }

    //将 xml数据转换为数组格式。
    private function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
                $subxml= $matches[2][$i];
                $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] =$this->xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }

    //请求数据到短信接口，检查环境是否 开启 curl init。
    private function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

}